﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.DataAccess;
using WarehouseManager.Model;

namespace WarehouseManager.View
{
    public partial class FormPhieuxuat : Form
    {
        PhieuxuatAccess phieuxuatAccess = new PhieuxuatAccess();
        KhachhangAcess khachhangAcess = new KhachhangAcess();
        NhanvienAccess nhanvienAccess = new NhanvienAccess();
        public FormPhieuxuat()
        {
            InitializeComponent();
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                txtMapx.ReadOnly = false;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;
                btnThem.Enabled = true;
            }
            else
            {
                txtMapx.ReadOnly = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
                btnThem.Enabled = false;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMapx.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            cbMakh.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            dtNgayxuat.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            cbManv.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        }
        public void loadMakh()
        {
            DataTable dt = new DataTable();
            dt = khachhangAcess.Laytoanbokh();
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                cbMakh.Items.Add(dr["maKH"].ToString());
            }
        }
        public void loadManv()
        {
            DataTable dt = new DataTable();
            dt = nhanvienAccess.Laytoanbonhanvien();
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                cbManv.Items.Add(dr["maNV"].ToString());
            }
        }
        private void FormPhieuxuat_Load(object sender, EventArgs e)
        {
            loadMakh();
            loadManv();
            dataGridView1.DataSource = phieuxuatAccess.laytoanbophieuxuat();
        }

        private void txtTimkiem_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = phieuxuatAccess.timkiemMapx(txtTimkiem.Text);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Phieuxuat pn = new Phieuxuat(txtMapx.Text, cbMakh.Text, DateTime.Parse(dtNgayxuat.Text), cbManv.Text);
            //if (phieuxuatAccess.themPhieuxuat(pn) == 0) { MessageBox.Show("Mã học sinh đã tồn tại !"); }
            //else
            //{
            //    MessageBox.Show("Bạn đã thêm thành công !");

            //}
            phieuxuatAccess.themPhieuxuat(pn);
            dataGridView1.DataSource = phieuxuatAccess.laytoanbophieuxuat();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Phieuxuat pn = new Phieuxuat(txtMapx.Text, cbMakh.Text, DateTime.Parse(dtNgayxuat.Text), cbManv.Text);
            phieuxuatAccess.suaPhieuxuat(pn);
            dataGridView1.DataSource = phieuxuatAccess.laytoanbophieuxuat();

        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Phieuxuat pn = new Phieuxuat(txtMapx.Text, cbMakh.Text, DateTime.Parse(dtNgayxuat.Text), cbManv.Text);
            phieuxuatAccess.xoaPhieuxuat(pn);
            dataGridView1.DataSource = phieuxuatAccess.laytoanbophieuxuat();

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                FormXuatHang formXuathang = new FormXuatHang(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                formXuathang.Show();
            }
        }
    }
}
